Dictionaries are a fundamental data structure in Python, allowing you to store and retrieve data using key-value pairs. Retrieving a value from a dictionary by its key is a common operation in many Python programs. In this article, we will explore five simple and commonly used methods to get a dictionary value by its key in Python.
Python Get Dictionary Value by KeyBelow, are the methods of Python Get Dictionary Value by Key in Python.
Using Bracket NotationUsing get() MethodUsing setdefault() MethodUsing Collections ModulePython Get Dictionary Value by Key Using Bracket NotationIn this example, a sample dictionary named `sample_dict` is created with key-value pairs representing a person's information. The value associated with the key 'name' is then accessed using bracket notation, and the result is printed as "Name: geeks".
Python3# Creating a sample dictionarysample_dict = {'name': 'geeks', 'age': 21, 'place': 'India'}# Accessing value using bracket notationname_value = sample_dict['name']print("Name:", name_value)OutputName: geeksPython Get Dictionary Value by Key Using get() MethodIn this example, a sample dictionary named `sample_dict` is created with key-value pairs. The `get()` method is then used to retrieve the value associated with the key 'name' safely, and the result is printed as "Name: geeks".
Python3# Creating a sample dictionarysample_dict = {'name': 'geeks', 'age': 21, 'place': 'India'}# Using get() methodname_value = sample_dict.get('name')print("Name:", name_value)OutputName: geeksPython Get Dictionary Value by Key Using setdefault() MethodIn this example, a sample dictionary named `sample_dict` is created. The `setdefault()` method is used to retrieve the value associated with the key 'name' and set a default value ('Default Name') if the key is not found. Additionally, `setdefault()` is used for a key ('country') not present in the dictionary, setting a default value ('Unknown').
Python3# Creating a sample dictionarysample_dict = {'name': 'geeks', 'age': 21, 'place': 'India'}# Using setdefault() methodname_value = sample_dict.setdefault('name', 'Default Name')print("Name:", name_value)# Using setdefault() for a key not present in the dictionarycountry_value = sample_dict.setdefault('country', 'Unknown')print("Country:", country_value)OutputName: geeksCountry: UnknownPython Get Dictionary Value by Key Using Collections ModuleIn this example, a `defaultdict` named `sample_dict` is created from the `collections` module with a default value of 'Unknown'. The value associated with the key 'place' is then accessed using bracket notation, and the result is printed as "Country: India".
Python3from collections import defaultdict# Creating a defaultdict with a default value of 'Unknown'sample_dict = defaultdict(lambda: 'Unknown', {'name': 'geeks', 'age': 21, 'place': 'India'})# Accessing value using bracket notationcountry_value = sample_dict['place']print("Country:", country_value)OutputCountry: IndiaConclusionIn conclusion, Python offers multiple methods to retrieve dictionary values by key, each with its own advantages. Choose the method that best fits your needs based on the specific requirements of your program. Whether you prefer simplicity, safety, or additional functionality, Python's rich set of tools allows you to work efficiently with dictionaries.